package com.os;

import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import mapgenerator.Main;

/**
 *
 * @author Sajarwo Anggai
 */
public class ObjectSpritePanelWidget {

    private Main comp;
    private DefaultMutableTreeNode top;
    private JScrollPane treeView;
    private JTree tree;

    public ObjectSpritePanelWidget(Main comp) {
        this.comp = comp;
        initComponents();
    }

    public void initComponents() {
        top = new DefaultMutableTreeNode("List of Sprite");
        tree = new JTree(top);
        treeView = new JScrollPane(tree);
        Dimension minimumSize = new Dimension(170, 400);
        treeView.setMinimumSize(minimumSize);
        treeView.setPreferredSize(minimumSize);
        tree.addTreeSelectionListener(comp.osControlPanel);
    }

    public void reloadAll() {
        initComponents();
        DefaultMutableTreeNode leafSprite = null;
        ArrayList al = comp.osContainer.getSprites();
        System.out.println("size = " + al.size());
        for (int i = 0; i < al.size(); i++) {
            ObjectSprite sprite = (ObjectSprite) al.get(i);
            leafSprite = addNode(sprite);
            if (sprite.isCharacter()) {
                leafSprite.add(addNode(ObjectSprite.LEFT_SPRITE));
                leafSprite.add(addNode(ObjectSprite.RIGHT_SPRITE));
                leafSprite.add(addNode(ObjectSprite.DOWN_SPRITE));
                leafSprite.add(addNode(ObjectSprite.UP_SPRITE));
            } else {
                leafSprite.add(addNode(ObjectSprite.OTHER_SPRITE));
            }
            top.add(leafSprite);
        }

        tree.expandRow(0);
        treeView.repaint();
        treeView.revalidate();
        comp.ObjectSpriteLeftPanelRender.removeAll();
        comp.ObjectSpriteLeftPanelRender.add(treeView);
        comp.ObjectSpriteLeftPanelRender.repaint();
        comp.ObjectSpriteLeftPanelRender.revalidate();        
        comp.ObjectSpriteRightPanelRender.setVisible(false);
        comp.osControlPanel.showImgeManagerInfo(false);
    }

    public DefaultMutableTreeNode addNode(Object obj) {
        return new DefaultMutableTreeNode(obj);
    }

    public JTree getTree() {
        return tree;
    }
}
